package com.bestcem.xm.componet.sms.push.service;

import com.bestcem.xm.componet.sms.SmsService;
import com.bestcem.xm.componet.sms.push.PushSmsProperties;
import com.bestcem.xm.componet.sms.push.core.PushSmsCommonApi;
import com.bestcem.xm.componet.sms.push.core.PushSmsSendApi;
import com.bestcem.xm.componet.sms.push.core.PushSmsServiceFactory;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "sms", name = "smsType", havingValue = "push")
public class PushSmsService implements SmsService, InitializingBean {

    //private final static String SMS_COMPONENT_CODE_KEY = "SMS_COMPONENT_CODE_KEY";

    private final PushSmsProperties pushSmsProperties;

    private final PushSmsServiceFactory factory;

    //private final RedisTemplate<String, Object> redisTemplate;

    private final ObjectMapper mapper = new ObjectMapper();

    @Override
    public Map<String, Object> sendSms(String mobile, String content, Map<String, Object> keywards) {
        log.info("PushSmsService sendSms mobile={},content={},keywards={}", mobile, content, keywards);
        if (!StringUtils.hasText(mobile) || !StringUtils.hasText(content)) {
            log.error("PushSmsService sendSms 手机号和短信内容必传");
            return new HashMap<>();
        }
        //PushBaseSetting baseSetting = pushSmsProperties.getBaseSetting();
        //String orgCode = baseSetting.getOrgCode();
        PushSmsSendApi sendService = factory.createSendService();
        Map<String, Object> info = new HashMap<>();
        //info.put("component_code", this.redisTemplate.opsForHash().get(SMS_COMPONENT_CODE_KEY, orgCode));
        info.put("content", content);
        info.put("phone", mobile);
        if (!ObjectUtils.isEmpty(keywards)) {
            keywards.remove("component_code");
            keywards.remove("content");
            keywards.remove("phone");
            info.putAll(keywards);
        }
        Map<String, Object> sendResult = sendService.sendSms(info);
        if (!Objects.equals(0, sendResult.get("code"))) {
            log.error("PushSmsService sendSms异常:{}", sendResult);
        } else {
            Map<String, Object> idmap = mapper.convertValue(sendResult.get("data"), new TypeReference<Map<String, Object>>() {
            });
            String id = idmap.get("id").toString();
            PushSmsCommonApi commonService = factory.createCommonService();
            Map<String, Object> smsRecord = commonService.getSmsRecord(id);
            if (!Objects.equals(0, smsRecord.get("code"))) {
                log.error("PushSmsService getSmsRecord异常:{}", smsRecord);
            } else {
                return mapper.convertValue(smsRecord.get("data"), new TypeReference<Map<String, Object>>() {
                });
            }
        }
        return new HashMap<>();
    }

    @Override
    public void afterPropertiesSet() {
        log.error("PushSmsService 实例化完成");
        //if (log.isInfoEnabled()) {
        //    PushSmsSendApi sendService = factory.createSendService();
        //    PushSmsCommonApi commonService = factory.createCommonService();
        //    PushBaseSetting baseSetting = pushSmsProperties.getBaseSetting();
        //    String orgCode = baseSetting.getOrgCode();
        //    //发一条短信测试一下
        //    String phone = StringUtils.hasText(baseSetting.getTestPhone()) ? baseSetting.getTestPhone() : "18255106958";
        //    Map<String, Object> info = new HashMap<>();
        //    //info.put("component_code", this.redisTemplate.opsForHash().get(SMS_COMPONENT_CODE_KEY, orgCode));
        //    info.put("content", String.format("%s使用push短信服务测试发送", orgCode));
        //    info.put("phone", phone);
        //    Map<String, Object> sendResult = sendService.sendSms(info);
        //    log.error("push发送短信:{}", sendResult);
        //    if (!Objects.equals(0, sendResult.get("code"))) {
        //        log.error("push发送短信异常:{}", sendResult);
        //    } else {
        //        Map<String, Object> map = mapper.convertValue(sendResult.get("data"), new TypeReference<Map<String, Object>>() {
        //        });
        //        String id = map.get("id").toString();
        //        Map<String, Object> smsRecord = commonService.getSmsRecord(id);
        //        log.error("push短信发送结果查询:{}", smsRecord);
        //    }
        //}
    }
}
